/**
 * jims
 */
package com.jims.master.modules.finance.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jims.master.modules.sys.entity.Dict;
import com.jims.master.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.finance.entity.ChargePriceSchedule;
import com.jims.master.modules.finance.service.ChargePriceScheduleService;

import java.util.List;
import java.util.Map;

/**
 * 收费系数Controller
 * @author 赵宁
 * @version 2017-06-12
 */
@Controller
@RequestMapping(value = "${adminPath}/finance/chargePriceSchedule")
public class ChargePriceScheduleController extends BaseController {

	@Autowired
	private ChargePriceScheduleService chargePriceScheduleService;
	
	@ModelAttribute
	public ChargePriceSchedule get(@RequestParam(required=false) String id) {
		ChargePriceSchedule entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = chargePriceScheduleService.get(id);
		}
		if (entity == null){
			entity = new ChargePriceSchedule();
		}
		return entity;
	}

	/**
	 * 优惠系数维护页面加载
	 * @param chargePriceSchedule
	 * @param request
	 * @param response
	 * @param model
	 * @return String
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ChargePriceSchedule chargePriceSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ChargePriceSchedule> page = chargePriceScheduleService.findPage(new Page<ChargePriceSchedule>(request, response), chargePriceSchedule);
		model.addAttribute("page", page);
		return "modules/finance/chargePriceIndex";
	}
	/**
	 * 根据费别加载优惠项目
	 * @param chargeValue
	 * @param chargePriceSchedule
	 * @param request
	 * @param response
	 * @param model
	 * @return String
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"loadChargeItems", ""})
    public String loadChargeItems(String chargeValue,ChargePriceSchedule chargePriceSchedule, HttpServletRequest request, HttpServletResponse response, Model model){
		if(chargeValue!=null){
			chargePriceSchedule.setChargeType(chargeValue);
		}
		Page<ChargePriceSchedule> page = chargePriceScheduleService.findPage(new Page<ChargePriceSchedule>(request, response), chargePriceSchedule);
		model.addAttribute("page", page);
		return "modules/finance/chargePriceScheduleIndex";
	}
	/**
	 * 组织树形结构数据
	 * @param extId
	 * @return List<Map<String, Object>>
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Dict> list= DictUtils.getDictList("CHARGE_TYPE_DICT");
		for (int i=0; i<list.size(); i++){
			   Dict e = list.get(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("value",e.getValue());
			    map.put("name",e.getLabel());
				mapList.add(map);
		}
		return mapList;
	}

	/**
	 * 收费系数维护新增，修改页面加载
	 * @param chargePriceSchedule
	 * @param model
	 * @return String
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ChargePriceSchedule chargePriceSchedule, Model model) {
		model.addAttribute("chargePriceSchedule", chargePriceSchedule);
		return "modules/finance/chargePriceScheduleForm";
	}

	/**
	 * 收费系数维护数据保存，修改
	 * @param chargePriceSchedule
	 * @param model
	 * @return StringData
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ChargePriceSchedule chargePriceSchedule, Model model) {
	    StringData data=new StringData();
		if(chargePriceSchedule.getPriceCoeffDreate()==null  || "".equals(chargePriceSchedule.getPriceCoeffDreate())){
			 chargePriceSchedule.setPriceCoeffDreate("0");
		}
		int i=chargePriceScheduleService.save(chargePriceSchedule);
        if(i>0){
            data.setCode("success");
            data.setData("保存收费系数成功");
        }else{
            data.setCode("error");
            data.setData("保存收费系数失败");
        }
        return data;
	}

	/**
	 * 收费系数维护删除
	 * @param chargePriceSchedule
	 * @return StringData
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ChargePriceSchedule chargePriceSchedule) {
	    StringData data=new StringData();
		int i=chargePriceScheduleService.delete(chargePriceSchedule);
        if(i>0){
            data.setCode("success");
            data.setData("删除收费系数成功");
        }else{
            data.setCode("error");
            data.setData("删除收费系数失败");
        }
		return data;
	}

}